//
//  NSObject+Common.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/11.
//

#import "NSObject+Common.h"

@implementation NSObject (Common)

#pragma mark 图片转字符串

+(NSString *)UIImageToBase64Str:(UIImage *)image
{
    NSData *data = UIImageJPEGRepresentation(image, .5f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encodedImageStr;
}

#pragma mark 字符串转图片
+(UIImage *)Base64StrToUIImage:(NSString *)_encodedImageStr
{
    NSData *_decodedImageData = [[NSData alloc] initWithBase64EncodedString:_encodedImageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
    UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
    return _decodedImage;

}

+ (NSString *)setTimeLabelWithTime:(long long)time {
    NSDate *lastDate = [NSDate dateWithTimeIntervalSince1970:(time/1000)];
    NSTimeInterval Interval = - [lastDate timeIntervalSinceNow];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd EEEE HH:mm"];
    NSString *nowDate = [dateFormatter stringFromDate:[NSDate date]];
    NSString *dateStr = [dateFormatter stringFromDate: lastDate];
    NSString *lastTime = [[dateStr componentsSeparatedByString:@" "]  lastObject];
    NSString *lastDay = [[dateStr componentsSeparatedByString:@" "] firstObject];
    NSString *lastWeek = [dateStr componentsSeparatedByString:@" "][1];
    NSString *nowDay = [[nowDate componentsSeparatedByString:@" "] firstObject];
    if ([nowDay isEqualToString:lastDay]) {
        return  lastTime;
    } else if(Interval < 24 * 60 * 60 ) {
        return @"昨天";
    } else if(Interval < 7 * 24 * 60 * 60) {
        return lastWeek;
    } else {
        return lastDay;
    }
}

@end
